<template>
  <div>
    <!-- 面包屑 -->
    <cru msg1="商品" msg2="商品"></cru>
    <!-- 添加分类 -->
    <el-dialog title="分配权限" :visible.sync="addDialogVisible" width="50%" @close="resetForm()">
      <el-form :rules="addCatRules" ref="addCatRef" :model="addCat" label-width="120px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCat.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类上级" prop="fenlei">
          <el-cascader
            expand-trigger="hover"
            :options="catTwoList"
            :props="catTwoListProps"
            v-model="catTwoSelected"
            @change="catTwoChange"
            change-on-select
            clearable
            style="width:100%"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcat()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-button type="primary" @click="showaddDialog()">添加分类</el-button>
      <el-table :data="catData" style="width: 100%" :stripe="true" :border="true" row-key="cat_id">
        <el-table-column prop="cat_name" label="分类名称"></el-table-column>
        <el-table-column prop="cat_level" label="等级" width="240">
          <template slot-scope="info">
            <el-tag v-if="info.row.cat_level == 0">一级</el-tag>
            <el-tag v-if="info.row.cat_level == 1" type="success">二级</el-tag>
            <el-tag v-if="info.row.cat_level == 2" type="warning">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240">
          <el-tooltip content="编辑" placement="top" :enterable="false">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="del(info.row.id)">编辑</el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" :enterable="false">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(info.row.id)">编辑</el-button>
          </el-tooltip>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.getList()
  },
  data() {
    return {
      catData: [],
      querycdt: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 添加
      addCat: {
        cat_name: '',
        cat_pid: '',
        cat_level: ''
      },
      addDialogVisible: false,
      addCatRules: {
        cat_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      catTwoList: [],
      catTwoListProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      catTwoSelected: []
    }
  },
  methods: {
    async getList() {
      const { data: dt } = await this.$http.get('categories', {
        params: this.querycdt
      })
      console.log(dt)
      if (dt.meta.status !== 200) {
        return this.$message.error(dt.meta.msg)
      }
      this.catData = dt.data.result
    },
    // 添加
    async showaddDialog() {
      this.addDialogVisible = true
      const { data: dt } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      console.log(dt)
      if (dt.meta.status !== 200) {
        return this.$message.error(dt.meta.msg)
      }
      this.catTwoList = dt.data
    },
    catTwoChange() {
      console.log(this.catTwoSelected)
      let len = this.catTwoSelected.length
      if (len === 0) {
        this.addCat.cat_pid = 0
        this.addCat.cat_level = 0
      } else {
        this.addCat.cat_pid = this.catTwoSelected[len - 1]
        this.addCat.cat_level = len
      }
    },
    addcat() {
      this.$refs.addCatRef.validate(async valid => {
        if (valid) {
          const { data: dt } = await this.$http.post('categories', this.addCat)
          if (dt.meta.status !== 201) {
            return this.$message.error(dt.meta.msg)
          }
          this.$message.success(dt.meta.msg)
          this.addDialogVisible = false
          this.getList()
        }
      })
    },
    resetForm() {
      this.$refs.addCatRef.resetFields()
      this.addCat.cat_pid = 0
      this.addCat.cat_level = 0
      this.catTwoSelected = []
    }
  }
}
</script>

<style lang="less" scoped>
</style>
